package com.yxy.common;

import org.apache.commons.lang3.StringUtils;

/**
 * API 返回 状态对象
 * Created by brucezhang on 10/01/2017.
 */
public class ResponseInfo<T> {
    public static final String SUCCESS = "success";
    public static final String ERROR = "error";
    /**
     * 返回信息
     */
    protected String message;

    /**
     * 返回状态码
     */
    protected int code;

    /**
     * 可以返回数据
     */
    protected Object data;

    /**
     * 错误信息
     */
    protected String error;

    public ResponseInfo success() {
        this.code = ResultCodes.SUCCESS;
        this.message = SUCCESS;
        return this;
    }

    /**
     * return success with result set
     *
     * @param data result set
     * @return ResultInfo
     */
    public ResponseInfo success(Object data) {
        this.code = ResultCodes.SUCCESS;
        this.message = SUCCESS;
        this.data = data;
        return this;
    }

    public ResponseInfo error() {
        this.code = ResultCodes.FAILED;
        this.message = ERROR;
//        this.data =
        return this;
    }

    /**
     * return success with result set
     *
     * @param errorCode errorCode
     * @param errReason result set
     * @return ResultInfo
     */
    public ResponseInfo error(Integer errorCode, String errReason) {
        String errMsg = "[Error Code:" + errorCode + " , message:" + errReason + "]";
        if (StringUtils.isBlank(errReason)) {
            errMsg = "error";
        }
        this.code = ResultCodes.FAILED;
        this.message = errMsg;
        this.data = errorCode;
        this.error = errReason;
        return this;
    }


    public ResponseInfo() {
    }

    public ResponseInfo(int code, String message, T data, String error) {
        this.message = message;
        this.code = code;
        this.data = data;
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("{");
        sb.append("message:'").append(message).append('\'');
        sb.append(", code:").append(code);
        sb.append(", data:").append(data);
        sb.append('}');
        return sb.toString();
    }
}
